import dt from '@dt/dt'
import comMgr from '@/biz/sales/salesCommon'
import buildingApi from '@/config/server/api/building.js'
import incentiveMgr from '@/biz/bonus/incentive'

let productTree = [], // 产品树
  buildingUnits = [], // 楼栋
  unitS = [], // 单元
  roomList = [];


class ProBuildUnit {
  constructor() {
    productTree = [] // 产品树
    buildingUnits = [] // 楼栋
    unitS = [] // 单元
    roomList = [];
  }
  getProductTreeInit() {
    let params = {
      housesId: parseInt(dt.buildInfo().housesId),
    }
    return comMgr.getProductTree(params).then((res) => {
      productTree = [...res]
      this.product.select.options = productTree
    })
  }

  getRoomList(unitId) {
    let params = {
      unitId,
      isPage: false
    }
    return incentiveMgr.roomList(params).then((res) => {
      if (res && res.list && res.list.length) {
        roomList = res.list
        roomList.forEach(element => {
          element.key = element.roomId
          element.value = element.roomCode
        })
        this.buildingGroup[0].group[2].select.options = roomList
      }

    })
  }

  options(_this) {
    let product = {
      key: 'productId',
      title: '所属产品',
      select: {
        options: productTree,
      },
      change: (data) => {
        _this.info.buildingId = ''
        _this.info.unitId = ''
        _this.info.roomId = ''
        if (!data.value) return false
        let proItem = productTree.filter(item => item.key == data.value)
        buildingUnits = proItem[0].buildingUnits
        buildingUnits.forEach(element => {
          element.key = element.buildingId
          element.value = element.buildingName
        })
        this.buildingGroup[0].group[0].select.options = buildingUnits
      },
    }

    let buildingGroup = [{
      key: 'houseName',
      title: '房源名称',
      group: [{
        key: 'buildingId',
        col: 8,
        placeholder: '楼栋',
        select: {
          options: buildingUnits
        },
        change: (data) => {
          _this.info.unitId = ''
          _this.info.roomId = ''
          if (!data.value) return false
          let proItem = buildingUnits.filter(item => item.key == data.value)
          console.log(proItem, 'unitSunitSunitSunitSunitS');
          unitS = proItem[0].unitS
          unitS.forEach(element => {
            element.key = element.unitId
            element.value = element.unitName
          })
          this.buildingGroup[0].group[1].select.options = unitS
        },
      }, {
        key: 'unitId',
        col: 8,
        placeholder: '单元',
        select: {
          options: unitS
        },
        change: (data) => {
          _this.info.roomId = ''
          if (!data.value) return false
          console.log(data, '选中单元');
          this.getRoomList(data.value)
        },
      }, {
        key: 'roomId',
        col: 8,
        placeholder: '房号',
        select: {
          options: roomList
        },
        change: (data) => {
          console.log(data, '选中房间');
        },
      }]
    }]
    this.product = product
    this.buildingGroup = buildingGroup
    this.getProductTreeInit()
    return [
      product, buildingGroup
    ]
  }
}

export default ProBuildUnit